//
//  HBMyInvitCodeCtrl.m
//  HuoBao
//
//  Created by 于会海 on 15/12/7.
//  Copyright © 2015年 jinxin. All rights reserved.
//

#import "HBMyInvitCodeCtrl.h"
#import "HBGetInviteCodeInfo.h"
#import "HBGetInviteCodeParam.h"
#import "HBGetInviteCodeParser.h"
#import "CustomCopySuccess.h"
#import "HBLoginManager.h"

@interface HBMyInvitCodeCtrl ()
{
    NSArray* labelArr;
    NSArray* summaryArr;
}

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *topViewToTopV;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *confirmToTopViewV;
@property (weak, nonatomic) IBOutlet UIImageView *topBack;

@property (weak, nonatomic) IBOutlet UIView *topView;
@property (weak, nonatomic) IBOutlet UILabel *invitationCode;
@property (weak, nonatomic) IBOutlet UIButton *reuseInvitCodeBtn;
@property (weak, nonatomic) IBOutlet UILabel *firstLabel;
@property (weak, nonatomic) IBOutlet UILabel *secondLabel;
@property (weak, nonatomic) IBOutlet UILabel *thirdLabel;

- (IBAction)reuseInvitCodeClicked:(UIButton *)sender;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *thirdLabelH;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *secLabelHeight;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *thirdToSecSpace;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *firstLabelH;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *sceLabelToFirstLabelV;
@end

@implementation HBMyInvitCodeCtrl

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:YES];
    [self.view setBackgroundColor:[LCCommon getColor:@"28292D"]];
    if (isIphone4or5) {
        self.topViewToTopV.constant = 15;
        self.confirmToTopViewV.constant = 15;
    }
    
    [self getInviteCode];
}

- (void)buildLayout{
    [super buildLayout];
  
    [self.reuseInvitCodeBtn setTitle:@"复制邀请码" forState:UIControlStateNormal];
    [self.reuseInvitCodeBtn setBackgroundColor:[LCCommon getColor:@"f34546"] forState:UIControlStateNormal];
    [self.reuseInvitCodeBtn setBackgroundColor:[LCCommon getColor:@"d43333"] forState:UIControlStateHighlighted];
    [self.reuseInvitCodeBtn.layer setMasksToBounds:YES];
    [self.reuseInvitCodeBtn.layer setCornerRadius:4.0];
    [self.topBack.layer setMasksToBounds:YES];
    [self.topBack.layer setCornerRadius:4.0];
    
    labelArr = @[self.firstLabel,
                 self.secondLabel,
                 self.thirdLabel,
                 ];
    summaryArr = @[@"邀请码为您注册金桔金融账号所使用的手机号；",
                   @"您可以点击“复制邀请码”按钮复制您的邀请码并发送给您的好友，好友注册时填写您的专属邀请码即与您绑定好友关系；",
                   @"好友绑定成功后您可以在邀请记录里查看您的好友列表并享受相关活动奖励。",
                  ];
    for (UILabel* label in labelArr) {
        [label setNumberOfLines:2];
        if ([label.text length] > label.frame.size.width) {
            
        }
    }
    for (int i =0; i <labelArr.count; i ++) {
        [self setLabel:labelArr[i] verSpeace:2.0 string:summaryArr[i]];
    }
     LLog(@"%@,%@,%@",self.firstLabel.text,self.secondLabel.text,self.thirdLabel.text);
    [self setNewFrame];
}

- (void)getInviteCode{
    HBGetInviteCodeParam* param = [[HBGetInviteCodeParam alloc] init];
    param.uid = [HBLoginManager sharedHBLoginManager].currentUserInfo.uid;
    param.token = [HBLoginManager sharedHBLoginManager].currentUserInfo.token;
    HBGetInviteCodeParser* parser = [[HBGetInviteCodeParser alloc] init];
    [NetCenter getDataFromURL:Method_getInviteCode params:param parser:parser result:^(ParserResult *myResult) {
        if (myResult.code == 0) {
            HBGetInviteCodeInfo* info = (HBGetInviteCodeInfo*)myResult.result;
            self.invitationCode.text = info.dataInfo;
            
        }else{
            DISSMISS_ERR(myResult.message);
        }
    } failure:^(NSError *error) {
        DISSMISS_ERR(error.description);
    }];
}

- (NSString*)getGID{
    NSUUID *identifierForVendor = [[UIDevice currentDevice] identifierForVendor];
    NSString *deviceId = [identifierForVendor UUIDString];
    return deviceId;
}

//自定义设置label行间距
- (void) setLabel:(UILabel*)label verSpeace:(CGFloat)verSpeace string:(NSString*)str{
    NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc]initWithString:str];
    NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc]init];
    //设置行距
    [style setLineSpacing:verSpeace];
    [attStr addAttribute:NSParagraphStyleAttributeName value:style range:NSMakeRange(0, [attStr length])];
    label.attributedText = attStr;
    label.numberOfLines = 2;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)reuseInvitCodeClicked:(UIButton *)sender {

    LLog(@"复制邀请码 -- -- %@",self.invitationCode.text);
    UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
    [pasteboard setString:self.invitationCode.text];
    
    CustomCopySuccess* copyView = [[CustomCopySuccess alloc] initWithCopyBtnOraginY:self.reuseInvitCodeBtn.origin.y nameString:@"复制成功"];

    [copyView  showSuccessAlertView];
}

- (void)setNewFrame{
    [self.view layoutIfNeeded];
    if (SCREEN_WIDTH == 414) {
        self.firstLabelH.constant = 27;
        self.sceLabelToFirstLabelV.constant = 18;
        self.secLabelHeight.constant  =  55;
        self.thirdToSecSpace.constant = 16;
        self.thirdLabelH.constant = 40;
    }
    if  (SCREEN_WIDTH < 414) {
        if (SCREEN_WIDTH == 375) {
            self.firstLabelH.constant = 25;
            self.sceLabelToFirstLabelV.constant = 17;
            self.secLabelHeight.constant  =  55;
        }
        if(SCREEN_WIDTH < 375){
            self.sceLabelToFirstLabelV.constant = 15;
            self.secLabelHeight.constant  =  50;
        }
        self.thirdToSecSpace.constant = 15;
        self.thirdLabelH.constant = 40;
    }
    self.secondLabel.numberOfLines = 0;
    [self.view layoutIfNeeded];
}

@end
